$(function () {
	$('.examinationAdd').on('hidden.bs.modal', function () {
		$('#examinationAdd')[0].reset();
		$('#examinationAdd input:hidden').val('');
		$('#examinationAdd .selectpicker').selectpicker('refresh');
		$('#projectId').val('');
		$('#projectId').trigger('chosen:updated');
		//$('#matchAdd .sysUser').remove();
		$('#examinationAdd').data('bootstrapValidator').resetForm();
	})
	$('.examinationEdit').on('hidden.bs.modal', function () {
		$('#examinationEdit')[0].reset();
		$('#examinationEdit input:hidden').val('');
		$('#examinationEdit .selectpicker').selectpicker('refresh');
		//$('#matchAdd .sysUser').remove();
		$('#examinationEdit').data('bootstrapValidator').resetForm();
	})
	
    //日期
    $(".form_datetime").jeDate({
        format: 'YYYY年MM月'
    });

    $('.date-picker').datepicker({
        language: 'zh-CN',
        format: 'yyyy-mm-dd'
    }).on('changeDate', function () {
        $(this).datepicker('hide');
    });
    //启用、禁用切换
    $('table tbody').on('click', '.status-btn', function () {
        if ($(this).hasClass('btn-use')) {
            $(this).removeClass('btn-use').addClass('btn-nouse');
            $(this).html('<i class="fa fa-ban"></i> 禁用');
        } else {
            $(this).removeClass('btn-nouse').addClass('btn-use');
            $(this).html('<i class="fa fa-check-square-o"></i> 启用');
        }
    })

    //新增
    educationStyle('.examinationAdd');

    //编辑
    educationStyle('.examinationEdit');

    /**
     * 项目类别是学历时显示教育形式
     * @param parentEle
     */
    function educationStyle(parentEle) {
        if ($(parentEle).find('.projectType :selected').val() == '2') {
            $(parentEle).find('.educationForm').parent().parent().show();
        } else {
            $(parentEle).find('.educationForm').parent().parent().hide();
        }

        $(parentEle).find('.projectType').change(function () {
            if ($(this).find(':selected').val() == '2') {
                $(parentEle).find('.educationForm').parent().parent().show();
            } else {
                $(parentEle).find('.educationForm').parent().parent().hide();
            }
        });
    }
    $('#updtprojectType').change(function(){
    	var projectType = $(this).val();
		$.ajax({
			url : ctx + '/bizProject/getAll',
			type: 'POST',
			data:{projectType:projectType},
			dataType: 'json',
			async:true,
			success: function (data) {
				var opt = "";
	        	for(var i=0; i<data.list.length; i++){
	        		opt += "<option value='"+data.list[i].projectId+"'>"+data.list[i].fullName+"</option>";
	        	}
	        	$("#updtprojectId").html('<option value="">--请选择--</option>'+opt);
			},
			error: function (response) {
	        	toastr.error("系统错误");
	        }
		});
    });
    $('#updtprojectId').change(function(){
    	var projectId = $(this).val();
		$.ajax({
			url: ctx + '/bizProjectLevel/getAllOption',
			type: 'POST',
			data:{projectId:projectId},
			dataType: 'json',
			async:true,
			success: function (data) {
				var opt = "";
	        	for(var i=0; i<data.list.length; i++){
	        		opt += "<option value="+data.list[i].projectLevelId+">" + data.list[i].levelTitle+ "</option>";
	        	}
	        	$("#updtprojectLevelId").html('<option value="">--请选择--</option>'+opt);
			},
			error: function (response) {
	        	toastr.error("系统错误");
	        }
		});
    });
})

//项目类型，项目联动
function selectProjectId(){
	var projectType = $('#projectType').find(':selected').val();
	$.ajax({
    	url : ctx + '/bizProject/getAll',
    	data:{projectType:projectType},
		dataType : 'json',
		type : 'post',
		success : function(data){
		var opt = "";
		for(var i=0; i<data.list.length; i++){
			opt += "<option value='"+data.list[i].projectId+"'>"+data.list[i].fullName+"</option>";
		}
		$('#projectId').html('<option value="">--请选择--</option>'+opt);
		$('#projectId').trigger('chosen:updated');
		$("#projectId").chosen({no_results_text: "没有匹配项",search_contains: true});
        $('.chosen-container').width('100%');
		},
    	error : function(){
			toastr.error("系统错误");
		}
    });
}
//项目，级别联动
function selectProjectLiveId(){
	var projectId= $('#projectId').find(':selected').val();
	$.ajax({
    	url : ctx + '/bizProjectLevel/getAllOption',
    	data:{projectId:projectId},
		dataType : 'json',
		type : 'post',
		success : function(data){
		var opt = "";
		for(var i=0; i<data.list.length; i++){
			opt += "<option value='"+data.list[i].projectLevelId+"'>"+data.list[i].levelTitle+"</option>";
		}
		$('#projectLevelId').html('<option value="">--请选择--</option>'+opt);
		},
    	error : function(){
			toastr.error("系统错误");
		}
    });
}

//新增
$('#examinationAdd').bootstrapValidator({
    submitHandler: function(validator, form, submitButton) {
    	var params = form.serialize();
    	$.ajax({
			url : ctx + '/bizExamSetting/addNewRecord',
			data : params,
			dataType : 'json',
			type : 'post',
			success : function(data){
				if(data.status != "success"){
					toastr.error(data.msg);
				}else{
					$('.examinationAdd').modal('hide');
					DataTable.init();
				}
			},
			error : function(){
				toastr.error("系统错误");
			}
		});
    	return false;
    }
});


DataTable.init();
/**
 * 回调函数
 * @param sSource
 * @param aoData
 * @param fnCallback
 * @returns
 */
function retrieveData( sSource, aoData, fnCallback, oSettings ) {
	aoData.push( { "name": "pageNum", "value": (Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength )+1) });
    aoData.push( { "name": "pageSize", "value": oSettings._iDisplayLength });  
    
    var searchVal = $('#searchVal').val();
    var enable = $('#enable').val();
    aoData.push({ "name": "enable", "value": enable });
    
    if(searchVal && searchVal.length != 0){
    	aoData.push({ "name": "searchVal", "value": searchVal});
    }
    
    $.ajax( {  
        "url": sSource,  
        "data": aoData,  
        "cache": false,  
        "dataType": 'json', 
        "type": "POST", 
        "success" :function(response) {
        	fnCallback(response.returnObject);
        }  
    } );  
};
//修改状态
function chooseStudent(val,flag){
	var attr = $("#span"+val).attr("class");
	if(attr == "btn btn-xs btn-nouse"){
		flag = 1;
	}else{
		flag = 0;
	}
	$.ajax({
        url: ctx + '/bizExamSetting/updateRecord',
        type: 'POST',
        data: {
        	examSettingId:val,
        	enable:flag
        },
        dataType: 'json',
        success: function (data) {
        	DataTable.init();
        }
	});
}
//编辑
function edit(examSettingId,examDate,projectType,projectId,projectLevelId,educationForm,disabledDate,endDate,clearDate){

    if (projectType == 2) {
        $('.examinationEdit').find('.educationForm').parent().parent().show();
    } else {
        $('.examinationEdit').find('.educationForm').parent().parent().hide();
    }

    $('.examinationEdit').find('.projectType').change(function () {
        if ($(this).find(':selected').val() == '2') {
            $('.examinationEdit').find('.educationForm').parent().parent().show();
        } else {
            $('.examinationEdit').find('.educationForm').parent().parent().hide();
        }
    });

	$('#updtexamSettingId').val(examSettingId);
	$('#updtexamDate').val(examDate);
	$.ajax({
        url: ctx + '/bizProject/getAll',
        type: 'POST',
        data:{"projectType":projectType},
        dataType: 'json',
        success: function (data) {
        	var opt = "";
        	for(var i=0; i<data.list.length; i++){
        		opt += "<option value="+data.list[i].projectId+">" + data.list[i].fullName+ "</option>";
        	}
        	$("#updtprojectId").html(opt);
        	$("#updtprojectId").val(projectId);
        	$('#updtprojectId').trigger('chosen:updated');
    		$("#updtprojectId").chosen({no_results_text: "没有匹配项",search_contains: true});
            $('.chosen-container').width('100%');
        },
        error: function (response) {
        	toastr.error("系统错误");
        }
    });
	$.ajax({
		url: ctx + '/bizProjectLevel/getAllOption',
		type: 'POST',
		data: {"projectId" : projectId},
		dataType: 'json',
		async:true,
		success: function (data) {
			var opt = "";
        	for(var i=0; i<data.list.length; i++){
        		opt += "<option value="+data.list[i].projectLevelId+">" + data.list[i].levelTitle+ "</option>";
        	}
        	$("#updtprojectLevelId").html(opt);
        	$("#updtprojectLevelId").val(projectLevelId);
		},
		error: function (response) {
        	toastr.error("系统错误");
        }
	});
	$('#updtprojectType').val(projectType);
	$('#updteducationForm').val(educationForm);
	$('#updtdisabledDate').val(disabledDate);
	$('#updtendDate').val(endDate);
	$('#updtclearDate').val(clearDate);
	
}
$('#examinationEdit').bootstrapValidator({
    submitHandler: function(validator, form, submitButton) {
    	var examSettingId = $('#examinationEdit').find('#updtexamSettingId').val();
    	var examDate = $('#examinationEdit').find('#updtexamDate').val();
    	var projectType = $('#examinationEdit').find('#updtprojectType').val();
    	var educationForm = $('#examinationEdit').find('#updteducationForm').val();
    	var disabledDate = $('#examinationEdit').find('#updtdisabledDate').val();
    	var endDate = $('#examinationEdit').find('#updtendDate').val();
    	var clearDate = $('#examinationEdit').find('#updtclearDate').val();
    	var projectId = $('#examinationEdit').find('#updtprojectId').val();
    	var projectLevelId = $('#examinationEdit').find('#updtprojectLevelId').val();
    	$.ajax({
			url : ctx + '/bizExamSetting/updateRecord',
			data : {examSettingId:examSettingId,examDate:examDate,projectType:projectType,projectId:projectId,projectLevelId:projectLevelId,
				educationForm:educationForm,disabledDate:disabledDate,endDate:endDate,clearDate:clearDate},
			dataType : 'json',
			type : 'post',
			success : function(data){
				if(data.status != "success"){
					toastr.error(data.msg);
				}else{
					$('.examinationEdit').modal('hide');
					DataTable.init();
				}
			},
			error : function(){
				toastr.error("系统错误");
			}
		});
    	return false;
    }
});
//回车搜索
function search(){
	if(event.keyCode==13){
		DataTable.init();
	}
}